<template>
  <view class="p-12 page_bg page_min_height">
    <DataLoading v-if="loading" />
    <template v-else>
      <template v-if="dataList.length">
        <view
          class="flex justify-between gap-x-12 p-12 border-b border-solid border-[#efefef] rounded mb-12 last:mb-0 last:border-b-0"
          v-for="item in dataList"
          :key="item.id"
        >
          <image class="w-42 h-42 rounded-full" src="/static/images/avatar.png" mode="aspectFill" />
          <view class="flex-1">
            <view class="flex justify-between items-center">
              <view class="text-base font-bold">工单进度</view>
              <view class="text-[#999] text-xs">2025-07-21 12:12</view>
            </view>
            <view class="mt-8 text-gray-400 line-clamp-2">
              消息内容消息内容消息内容消息内容消息内容消息内容消息内容消息内容消息内容消息内容消息内容消息内容消息内容消息内容
            </view>
          </view>
        </view>
      </template>
      <template v-else-if="userIdentity.isLogin">
        <EmptyData v-if="userIdentity.isCustomer" />
        <NotCustomer v-else />
      </template>
      <NotLogin v-else />
    </template>
  </view>
</template>

<script setup>
import { ref } from 'vue';
import { onLoad, onReachBottom, onPullDownRefresh } from '@dcloudio/uni-app';
import { getNoticeListApi } from '@/api/common';
import { usePage, useUserIdentity } from '@/utils/usePublic';

const loading = ref(true);
const dataList = ref([]);
const pageParams = usePage();
const userIdentity = useUserIdentity();

onLoad(() => {
  if (userIdentity.isCustomer) {
    initData();
  } else {
    loading.value = false;
  }
});

onReachBottom(() => {
  if (pageParams.isLoadAll || !userIdentity.isCustomer) {
    return;
  }
  pageParams.page++;
  getDataList();
});

onPullDownRefresh(() => {
  setTimeout(() => {
    initData();
    uni.stopPullDownRefresh();
  }, 500);
});

function initData() {
  pageParams.page = 1;
  pageParams.isLoadAll = false;
  dataList.value = [];
  getDataList();
}

async function getDataList() {
  try {
    const { code, data } = await getNoticeListApi({ ...pageParams });
    loading.value = false;
    if (code === 200) {
      dataList.value = dataList.value.concat(data.list);
      pageParams.isLoadAll = data.list.length < pageParams.limit;
    }
  } catch (e) {
    loading.value = false;
    console.error(e);
  }
}
</script>
